﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GSMClasses
{
    public class GSM
    {
        //fields
        private Battery myBattery = new Battery();
        private Display myDisplay = new Display();
        private string model;
        private string manufacturer;
        private float? price;
        private string owner;

        //static field
        static private GSM iphone = new GSM("IPhone4S", "Apple");

        //constructors
        public GSM(string model, string manufacturer)
            : this(model, manufacturer, null)
        {
        }
        public GSM(string model, string manufacturer, float? price)
            : this(model, manufacturer, price, null)
        {
        }
        public GSM(string model, string manufacturer, float? price, string owner)
            : this(model, manufacturer, price, owner, null)
        {
        }
        public GSM(string model, string manufacturer, float? price, string owner, Battery myBattery)
            : this(model, manufacturer, price, owner, myBattery, null)
        {
        }
        public GSM(string model, string manufacturer, float? price, string owner, Battery myBattery, Display myDisplay)
        {
            this.Model = model;
            this.Manufacturer = manufacturer;
            this.Price = price;
            this.Owner = owner;
            this.myBattery = myBattery;
            this.myDisplay = myDisplay;
        }

        //properties
        public List<Call> callHistory = new List<Call>();

        public string Model
        {
            get { return this.model; }
            set { this.model = value; }
        }

        public string Manufacturer
        {
            get { return this.manufacturer; }
            set { this.manufacturer = value; }
        }

        public float? Price
        {
            get { return this.price; }
            set
            {
                if (value >= 0 || value == null)
                {
                    this.price = value;
                }
                else
                {
                    throw new ArgumentOutOfRangeException();
                }
            }
        }

        public string Owner
        {
            get { return this.owner; }
            set { this.owner = value; }
        }

        public static GSM Iphone
        {
            get
            {
                return iphone;
            }
        }

        //methods
        public override string ToString()
        {
            StringBuilder infoText = new StringBuilder();
            infoText.Append("model:");
            infoText.AppendLine(this.model);
            infoText.Append("manufacturer:");
            infoText.AppendLine(this.manufacturer);
            if (price != null)
            {
                infoText.Append("price:");
                infoText.AppendLine(this.price.ToString());
            }
            if (owner != null)
            {
                infoText.Append("owner:");
                infoText.AppendLine(this.owner);
            }
            if (myDisplay != null)
            {
                infoText.Append(this.myDisplay.ToString());
            }
            if (myBattery != null)
            {
                infoText.AppendLine(this.myBattery.ToString());
            }
            return infoText.ToString();
        }

        public void AddCall(Call newCall)
        {
            callHistory.Add(newCall);
        }

        public void DeleteCall(int index)
        {
            callHistory.RemoveAt(index);
        }

        public void ClearHistory()
        {
            callHistory.Clear();
        }

        public float CalculateTotalPrice(GSM phone, float price)
        {
            float totalDuration = 0;
            for (int i = 0; i < phone.callHistory.Count; i++)
            {
                totalDuration += phone.callHistory[i].Duration / 60;
            }
            return totalDuration * price;
        }

        public void PrintCallHistory()
        {
            for (int i = 0; i < callHistory.Count; i++)
            {
                Console.WriteLine(callHistory[i].DateAndTime);
                Console.WriteLine(callHistory[i].DiledNumber);
                Console.WriteLine(callHistory[i].Duration + "seconds");
                Console.WriteLine();
            }
        }

    }
}
